package com.ghsc.ghxc.jsb.ui.live.adapter

import android.text.TextUtils
import androidx.recyclerview.widget.RecyclerView
import com.aliyun.auiappserver.model.LiveModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cn.appcore.ext.loge
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.flyco.roundview.RoundRelativeLayout
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.ui.live.bean.LiveExtendDataBean
import com.ghsc.ghxc.jsb.ui.live.view.LiveBlackList.numberMultiple
import com.ghsc.ghxc.jsb.utils.StringUtil
import java.util.Locale

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.live.adapter
 * 类名：LiveListAdapter
 * 创建时间：2024/5/10 11:53
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class LiveListAdapter  (width: Float)  : BaseQuickAdapter<LiveModel, BaseViewHolder>(R.layout.fragment_find_item) {
    var width: Float = 0f
    var numCount: Int = 0
    fun setAddNumCount(numCount:Int){
        this.numCount=numCount
    }
    init {
        this.width = width
    }

    // 预加载回调
    var onPreload: (() -> Unit)? = null

    // 预加载偏移量
    var preloadItemCount = 0

    // 列表滚动状态
    private var scrollState = RecyclerView.SCROLL_STATE_IDLE

//    var numCount :Int = 0



    override fun convert(helper: BaseViewHolder, item: LiveModel) {
//        loge("xxx"+width/2)
        var llItem = helper.getView<RoundRelativeLayout>(R.id.ll_item)
        val para1 = llItem.layoutParams
        para1.width = StringUtil.dp2px((width / 2)).toInt() - StringUtil.dp2px(8f).toInt()
        llItem.layoutParams = para1
//        直播状态，0-准备中，1-已开始，2-已结束
        helper
            .setText(R.id.title,item.title)
//            .setText(R.id.liveNum,item.metrics.pv.toString()+" 人观看")

//            .setText(R.id.liveNum,   formatNumber(11215)+" 人看过")
//            .setText(R.id.liveStatus,if (item.status==0) "-准备中" else if (item.status==1) "已开始" else "已结束")


//        if (item.liveCount>0){
//            helper.setText(R.id.liveNum,   formatNumber(item.metrics.pv *  item.liveCount)+" 人看过")
//        }else {
//            helper.setText(R.id.liveNum,   formatNumber(item.metrics.pv* if (numCount==0) 7 else numCount)+" 人看过")
//        }
        helper.setText(R.id.liveNum,   formatNumber(item.metrics.pv* numCount)+" 人看过")

        //封面。
        val extendJson: String = item.extend
        val bean = GsonUtil.GsonToJsonClass(
            extendJson,
            LiveExtendDataBean::class.java
        )
        GlideUtil.ImageLoad(this.context, bean.userAvatar, helper.getView(R.id.imgHead))
        helper.setText(R.id.name,bean .userNick)
        if (TextUtils.isEmpty(bean.cover_url)){
            helper .setImageResource(R.id.cover_url,R.mipmap.ic_launcher)
        }else
            GlideUtil.ImageLoad(this.context, bean.cover_url, helper.getView(R.id.cover_url))
    }







    private fun formatNumber(number: Int): String? {
        return if (number < 0) {
            // 兜底保护
            0.toString()
        } else if (number >= 10000) {
            // 1w+ 格式化
            //            setRefreshCount(String.format(Locale.getDefault(), "%.1fw", number / 10000f) );
            String.format(Locale.getDefault(), "%.1fw", number / 10000f)
        } else {
            number.toString()
        }
    }
//    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
//        super.onBindViewHolder(holder, position)
//        if ((itemCount-preloadItemCount) == position){
//            onPreload?.invoke()
//        }
//    }






}
